package com.smallcc.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smallcc.dataobject.OrderDetail;
import com.smallcc.dto.OrderDTO;
import com.smallcc.enums.ResultEnum;
import com.smallcc.exception.SellException;
import com.smallcc.form.OrderForm;
import com.smallcc.util.ExceptionUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 晓风过境
 * @TODO
 * @date 2018/3/29 22:37
 * @day 29
 * @return $text$
 * @title ${CLASS_NAME}
 * @project_name sell
 */
@Data
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDTO converter(OrderForm orderForm){
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();

        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());

        List<OrderDetail> orderDetailList=new ArrayList<>();

        try {
            orderDetailList=gson.fromJson(orderForm.getItems(),new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e){
            log.error("[对象转换]错误,String={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),ExceptionUtil.getStackTrace(e));

        }

        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }

}
